<template>
  <div>
    <el-form :inline="true" :model="from" class="demo-form-inline">
      <el-form-item label="品牌名称">
        <el-input v-model="from.keyWord" placeholder="请输入品牌名" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="listHh()">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-button type="primary" @click="addBrandFrom()">添加</el-button>
  </div>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="brandId" label="id" width="180" />
      <el-table-column prop="brandName" label="名称" width="180" />
      <el-table-column prop="useCount" label="使用数量" />
      <el-table-column prop="brandTop" label="排序" />
      <el-table-column prop="brandStatus" label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.brandStatus"
            inline-prompt
            active-text="显示"
            inactive-text="隐藏"
            :active-value="1"
            :inactive-value="0"
            @change="onchangeStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="注册时间" />
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="updateTime" label="修改时间" />
      <el-table-column prop="updateBy" label="修改人" />
    </el-table>
  </div>
  <div>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="from.pageNum"
        v-model:page-size="from.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" title="添加弹出框" width="500">
      <el-form :model="form" ref="ruleFormRef" :rules="rules">
        <el-form-item
          label="品牌名称"
          :label-width="formLabelWidth"
          prop="brandName"
        >
          <el-input v-model="form.brandName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="排序"
          :label-width="formLabelWidth"
          prop="brandTop"
        >
          <el-input-number v-model="form.brandTop" :min="1" />
        </el-form-item>
        <el-form-item
          label="状态"
          :label-width="formLabelWidth"
          prop="brandStatus"
        >
          <el-radio-group v-model="form.brandStatus">
            <el-radio :value="0" size="large"> 显示</el-radio>
            <el-radio :value="1" size="large"> 隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
const tableData = ref([]);
import { onMounted, ref, reactive } from "vue";
import {
  selectList,
  getBrandList,
  addBrand,
  updateBrandStatus,
} from "../api/api";
import type { ComponentSize } from "element-plus";
import type { UploadProps } from "element-plus";
const value3 = ref(true);
const imageUrl = ref("");
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";

const headers = ref({
  token: sessionStorage.getItem("token") || "",
});

const onchangeStatus = (row: any) => {
  console.log("onChageStatus", row);
  updateBrandStatus(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const form = ref({
  brandId: null,
  brandName: "",
  useCount: null,
  brandTop: null,
  brandStatus: null,
  createTime: "",
  createBy: "",
  updateTime: null,
  updateBy: null,
});

const ruleFormRef = ref<FormInstance>();

const rules = reactive<FormRules<any>>({
  brandName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
  brandTop: [
    {
      required: true,
      message: "请输入顺序",
      trigger: "blur",
    },
  ],
  brandStatus: [{ required: true, message: "请选择状态", trigger: "change" }],
});

const addBrandFro = () => {
  console.log(form.value);

  addBrand(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      listHh();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const addBrandFrom = () => {
  dialogFormVisible.value = true;
};

const from = ref({
  beginTime: "",
  endTime: "",
  gender: null,
  keyWord: "",
  pageNum: 1,
  pageSize: 2,
});
const total = ref(0);
const size = ref<ComponentSize>("default");
const background = ref(false);
const disabled = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  from.value.pageSize = val;
  listHh();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  from.value.pageNum = val;
  listHh();
};

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      addBrandFro();
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const listHh = () => {
  getBrandList(from.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
listHh();
</script>
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
